package com.dailyblue.javaee.controller;

import com.dailyblue.javaee.bean.Emp;
import com.dailyblue.javaee.service.EmpService;
import com.dailyblue.javaee.service.impl.EmpServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

@WebServlet("/emp_find_enname")
public class EmpFindByEnnameController extends HttpServlet {

    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {

        HttpSession session = request.getSession();
        System.out.println(11111);
        request.setCharacterEncoding("utf-8");
        String enname = request.getParameter("enname");
        String p = request.getParameter("page");
        String type = request.getParameter("type");
        System.out.println(enname+"(())");
        if (session.getAttribute("TYPE") != null) {
            if (enname == null) {
                enname = session.getAttribute("ENNAME").toString();
            }
        }
        int page = 1;
        if (p != null) {
            page = Integer.parseInt(p);
        }
        EmpService service = new EmpServiceImpl();
        List<Emp> list = service.findByEnname(enname, page);
        int pageNum = service.findAllPageNum(enname);
        session = request.getSession();
        session.setAttribute("LIST", list);
        session.setAttribute("PAGENUM",pageNum);
        session.setAttribute("TYPE", type);
        session.setAttribute("ENNAME", enname);
        response.sendRedirect("emp.jsp");
    }
}